<?php


namespace app\admin\controller;

use app\admin\controller\Common;

//use think\Controller;
use think\Db;
use think\Validate;

class Conf extends Common
{
    public function conflst()
    {
        if(request()->ispost()){
          $data =input('post.');
          $enameArr = db('conf')->column('ename');
          $confArr = array();
            //附件类型数据处理
            $imgcolumn = db('conf')->where('dt_type',6)->column('ename');
          foreach ($data as $k => $v){
              $confArr[] =$k;
              if (is_array( $v)){
                  $v = implode(',',$v);
              }
             db('conf')->where('ename',$k)->update(['value'=>$v]);
          }
          foreach ($enameArr as $k =>$v){  //判断上传的附件可以为空，不对其修改
              if(!in_array($v,$confArr) && !in_array($v,$imgcolumn)){
             db('conf')->where('ename',$k)->update(['value'=>'']);
              }

              foreach ($imgcolumn as $k => $v){
                  if ($_FILES[$v] !=''){
                      $file = request()->file($v);
                      // 移动到框架应用根目录/uploads/ 目录下
                      $info = $file->move( '../uploads');
                      $imfSrc = $info->getExtension();
                      db('conf')->where('ename',$v)->update(['value'=>$imfSrc]);
                  }
              }
          }
          $this->success('修改配置项成功！',url('conflst'));
        }

        $confRes = Db::name('conf')->select();
        $this->assign('confRes',$confRes);

        return view();

    }

    public function lst()
    {
        $confRes = db('conf')->field('id,cname,ename,value,values')->paginate(10);
        $this->assign('confRes',$confRes);
        return view();

    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
          $validate = validate('conf');
          if (!$validate->scene('add')->check($data)){

              $this->error($validate->getError());
          }
            $add = Db::name('conf')->insert($data);
            if ($add) {
                $this->success('添加配置项城成功！', 'lst', '', '5');
            } else {
                $this->error('添加配置项城失败！', 'lst', '', '5');
            }
        }
        return view();

    }

    public function edit($id)
    {
        if(request()->isPost()){
            $data = input('post.');
            $validate = validate('conf');
            if (!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $save = Db::name('conf')->where('id',$id)->update($data);
            if ($save){
                $this->success('更新成功！','lst','','5');
            }else{
                $this->error('更新失败！','lst','','5');
            }
        }
        $confs = Db::name('conf')->find($id);
        $this->assign('conf',$confs);
        return view();

    }

    public function del($id)
    {
        $res = Db::name('conf')->where('id',$id)->delete();
           if ($res){
               $this->success('删除配置成功！','lst','','5');
           }else{
               $this->error('删除配置项失败！');
       }
    }



}